package com.hmxx.myblogproject.service.impl;

import com.hmxx.myblogproject.entity.MyBlogFollow;
import com.hmxx.myblogproject.entity.MyBlogUser;
import com.hmxx.myblogproject.mapper.MyBlogFollowMapper;
import com.hmxx.myblogproject.mapper.MyBlogUserMapper;
import com.hmxx.myblogproject.service.MyBlogUserService;
import com.hmxx.myblogproject.util.JwtHelper;
import com.hmxx.myblogproject.util.MD5Util;
import com.hmxx.myblogproject.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.interfaces.ECKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @Author: {27106}
 * @Date: {2021/11/16 9:23}
 */
@Service
public class MyBlogUserServiceImpl implements MyBlogUserService {
    @Resource
    private MyBlogUserMapper myBlogUserMapper;
    @Resource
    private MyBlogFollowMapper myBlogFollowMapper;
    /**
     * @Description 登录
     * @Param:
     * @return:
     * @Author: 27106
     * @Date: 2021/11/16
     */
    @Override
    public Object queryUser(MyBlogUser myBlogUser) {

        MyBlogUser result = myBlogUserMapper.queryUserByNameAndPwd(myBlogUser.getUsername(), null);
        if (null == result) {
            return ResponseUtil.fail(2, "用户名不存在");
        }
        if (MD5Util.getMD5Str(myBlogUser.getPassword()).equals(result.getPassword())) {
            String token = JwtHelper.generateToken(Integer.valueOf(result.getId()));
            int size=myBlogFollowMapper.queryFollowsByUId(result.getId());
            result.setFans(String.valueOf(size));
            Map map = new HashMap<>();
            map.put("user", result);
            map.put("token", token);
            return ResponseUtil.ok(map);
        } else {
            return ResponseUtil.fail(1, "密码错误");
        }

    }

    /**
     * @Description 注册
     * @Param:
     * @return:
     * @Author: 27106
     * @Date: 2021/11/16
     */
    @Override
    public Object register(MyBlogUser myBlogUser) {
        MyBlogUser result = myBlogUserMapper.queryUserByNameAndPwd(myBlogUser.getUsername(), null);
        if (result != null) {
            return ResponseUtil.fail(1, "用户已存在");
        }
        myBlogUser.setPassword(MD5Util.getMD5Str(myBlogUser.getPassword()));
        int res = myBlogUserMapper.insertUserWithNameAndPwd(myBlogUser);
        if (res > 0) {
            String token = JwtHelper.generateToken(Integer.valueOf(myBlogUser.getId()));
            Map map = new HashMap();
            map.put("token", token);
            map.put("user", myBlogUser);
            return ResponseUtil.ok(map);
        } else {
            return ResponseUtil.fail(2, "注册失败");
        }
    }

    @Override
    public Object loginStatus(HttpServletRequest request) {
        if (null != JwtHelper.getUserId(request.getHeader("MyBlog-Token"))) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail(2, "用户登录过期，请重新登录");
        }
    }

}
